package com.i2bt.app.set.model;

import com.i2bt.common.AndroidRestContract;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.set.AuthoritySetActivity;
import com.i2bt.bean.AuthoritySetBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SetAuthorityJsonHandle extends BaseJsonModel {
	AuthoritySetActivity act;

	public SetAuthorityJsonHandle(BaseActivity activity) {
		super(activity);
		this.act = (AuthoritySetActivity) activity;
		// TODO Auto-generated constructor stub
	}

	@Override
	protected String logicToWrap() throws JSONException {
		// TODO Auto-generated method stub
		AuthoritySetBean bean = (AuthoritySetBean) this.beanParam;
		try {
			jsonParam.put(AndroidRestContract.ACTION, bean.getAct());
			jsonParam.put(AndroidRestContract.TOKEN, bean.getToken());
			JSONArray state = new JSONArray();
			String s[] = bean.getHomeboxState();
			String id[] = bean.getHomeboxId();
			for (int i = 0; i < s.length; i++) {
				JSONObject obj = new JSONObject();
				obj.put(AndroidRestContract.HOMEBOX_ID, id[i]);
				obj.put(AndroidRestContract.HOMEBOX_POWER, s[i]);
				state.put(obj);
			}
			jsonParam.put(AndroidRestContract.HOMEBOX_LIST, state);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		// TODO Auto-generated method stub
		System.out.println(result);
		if (isSuc) {
			try {
				JSONObject ja = new JSONObject(result);
				boolean succ = ja.getBoolean(AndroidRestContract.SUCCEED);
				String mes = null;
				if (succ) {
					this.act.finish();
					System.out.println("保存成功!");
				} else {
					mes = ja.getString(AndroidRestContract.MESSAGE);
					System.out.println("保存失败:"+mes);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			this.act.toastHandler("网络连接失败");
		}
	}

}
